package cn.donting.jme.edit.attribute;

import com.jme3.math.ColorRGBA;
import javafx.scene.Node;
import javafx.scene.control.ColorPicker;
import javafx.scene.paint.Color;

public class ColorRGBAAttribute extends Attribute<ColorRGBA> {

   private ColorPicker colorPicker = new ColorPicker();

    public ColorRGBAAttribute(String title) {
        super(title);
        colorPicker.valueProperty().addListener(
                (observable, oldValue, newValue) ->
                        setSuperValue(colorToColorRGBA(newValue))
        );
    }

    public ColorRGBAAttribute() {
        this("");
    }

    @Override
    public void setValue(ColorRGBA value) {
        colorPicker.setValue(colorRGBAToColor(value));
    }

    private void setSuperValue(ColorRGBA value) {
        super.setValue(value);
    }

    @Override
    public Node getUINode() {
        return colorPicker;
    }

    public static ColorRGBA colorToColorRGBA(Color color) {
        ColorRGBA colorRGBA = new ColorRGBA((float) color.getRed(), (float) color.getGreen(), (float) color.getBlue(), (float) color.getOpacity());
        return colorRGBA;
    }

    public static Color colorRGBAToColor(ColorRGBA colorRGBA) {
        Color color = new Color(colorRGBA.getRed(), colorRGBA.getGreen(), colorRGBA.getBlue(), colorRGBA.getAlpha());
        return color;
    }
}
